package org.karelun.whiteboard.edit.properties;

import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.karelun.whiteboard.Dimension;
import org.karelun.whiteboard.WhiteboardPackage;

/**
 * @Author: KarelunHuang
 * @Date: Jan 30, 2013
 **/
public class SizePropertySection extends BasePropertySection<Dimension> {

	private Text heightText;
	
	private Text widthText;
	
	private ISWTObservableValue hTextObserver;
	
	private ISWTObservableValue wTextObserver;
	
	public SizePropertySection() {
	}

	@Override
	protected String getSectionTitle() {
		return "Size";
	}

	@Override
	protected void doCreateControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(4, false));
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		new Label(composite, SWT.NONE).setText("Height:");
		heightText=new Text(composite, SWT.BORDER);
		heightText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		hTextObserver=WidgetProperties.text(SWT.Modify).observe(heightText);
		
		new Label(composite, SWT.NONE).setText("Width:");
		widthText=new Text(composite, SWT.BORDER);
		widthText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		wTextObserver=WidgetProperties.text(SWT.Modify).observe(widthText);
	}

	@Override
	protected void dataBinding() {
		Dimension size = getSelectedObjectModel();
		dataBinding(hTextObserver, size,WhiteboardPackage.Literals.DIMENSION__HEIGHT,Integer.class);
		dataBinding(wTextObserver, size,WhiteboardPackage.Literals.DIMENSION__WIDTH,Integer.class);
	}
	
	@Override
	public void refresh() {
		if(heightText!=null&&!heightText.isDisposed()){
			heightText.setEnabled(isEditable());
		}
		if(widthText!=null&&!widthText.isDisposed()){
			widthText.setEnabled(isEditable());
		}
	}
}
